{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Face++ FaceSets 实践\n",
    "\n",
    "\n",
    ">* 1. FaceSet Create\n",
    ">* 2. FaceSet GetDetail\n",
    ">* 3. FaceSet AddFace\n",
    ">* 4. FaceSet RemoveFace\n",
    ">* 5. FaceSet Update\n",
    ">* 6. Compare Face"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_secret = \"o0f98faN25ofaxKjwnYDffZj3wRcL6a4\"\n",
    "api_key =  'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis' # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet Create（创建人脸集合）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json\n",
    "\n",
    "display_name = \"众星\"\n",
    "outer_id = \"shili02\"\n",
    "user_data = \"随便\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'display_name':'众星',\n",
    "    'outer_id':'shili02',\n",
    "    'user_data':'随便'\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(CreateFace_Url, params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa8d1e1195452303b0a5ebdf15622a27',\n",
       " 'time_used': 223,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603382816,9b33ef4a-8f8f-4fd6-9fb2-fa7d407044b7',\n",
       " 'outer_id': 'shili02',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet GetDetail（获取人脸集合信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'outer_id':'shili02',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(GetDetail_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa8d1e1195452303b0a5ebdf15622a27',\n",
       " 'tags': '',\n",
       " 'time_used': 91,\n",
       " 'user_data': '随便',\n",
       " 'display_name': '众星',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603382955,4be1e4c0-bb96-4f82-82cd-8f28490dc3a3',\n",
       " 'outer_id': 'shili02'}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://l_shi_li.gitee.io/api-stores-things/hb.jpg'\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "payload = {\n",
    "   \"image_url\":'http://l_shi_li.gitee.io/api-stores-things/hb.jpg',\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603385318,77e4c02b-6a99-4290-aa9f-aca7e7191295',\n",
       " 'time_used': 333,\n",
       " 'faces': [{'face_token': '5be91cdcb52f34981f4d97a8283441a7',\n",
       "   'face_rectangle': {'top': 33, 'left': 127, 'width': 240, 'height': 240},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 0.033, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.004,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.021,\n",
       "     'neutral': 0.36,\n",
       "     'sadness': 99.614,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'aEXaGml7aQG0yrw+kmlBYg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json() \n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet AddFace（增加人脸信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'faceset_token': 'fa8d1e1195452303b0a5ebdf15622a27',\n",
    "    'face_tokens':'5be91cdcb52f34981f4d97a8283441a7',\n",
    "\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa8d1e1195452303b0a5ebdf15622a27',\n",
       " 'time_used': 439,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603385348,08711a3e-ffe0-4264-96a9-e6031b47aab4',\n",
       " 'outer_id': 'shili02',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet RemoveFace（移除人脸信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'37071d95016c1b2d81591a6f0c1681f2',\n",
    "    'face_tokens':'b0407b9e803ebd39d511cd7956fd5bf5',\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(RemoveFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '37071d95016c1b2d81591a6f0c1681f2',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 175,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1602155720,7a6b9a32-08ca-4c14-981d-d4ffbd82da48',\n",
       " 'outer_id': '00001',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FaceSet Update（更新人脸集合信息）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'faceset_token': 'fa8d1e1195452303b0a5ebdf15622a27',\n",
    "    'user_data':\"随便\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Update_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fa8d1e1195452303b0a5ebdf15622a27',\n",
       " 'request_id': '1603385532,987b686c-2941-4391-ab02-7c1a50abf931',\n",
       " 'time_used': 81,\n",
       " 'outer_id': 'shili02'}"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare Face（对比人脸相似度）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [],
   "source": [
    "科比 = \"http://l_shi_li.gitee.io/api-stores-things/kobe.jpg\"\n",
    "黄渤 = \"http://l_shi_li.gitee.io/api-stores-things/hb.jpg\"\n",
    "岳云鹏 = \"http://l_shi_li.gitee.io/api-stores-things/yyp.jpg\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方案1:直接对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [],
   "source": [
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'image_url1':\"http://l_shi_li.gitee.io/api-stores-things/hb.jpg\",\n",
    "    'image_url2':\"http://l_shi_li.gitee.io/api-stores-things/kobe.jpg\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 240,\n",
       "    'top': 33,\n",
       "    'left': 127,\n",
       "    'height': 240},\n",
       "   'face_token': '9b0f290bb15236a91efd638524754b96'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 248,\n",
       "    'top': 157,\n",
       "    'left': 139,\n",
       "    'height': 248},\n",
       "   'face_token': 'b7d9aa74ab620999f9bdb6408f58c2c2'}],\n",
       " 'time_used': 858,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 22.82,\n",
       " 'image_id2': 'cqOI2ncCuUdsj3ernNrNvA==',\n",
       " 'image_id1': 'aEXaGml7aQG0yrw+kmlBYg==',\n",
       " 'request_id': '1603387997,a637c5a1-b552-481c-b838-c5349a0a6a55'}"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方案2:与人脸集合进行对比"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 面部检测(获取face_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [],
   "source": [
    "Detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url ='http://l_shi_li.gitee.io/api-stores-things/hb.jpg'\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":'http://l_shi_li.gitee.io/api-stores-things/hb.jpg',\n",
    "    'api_key': 'j6dZuhzZPUpXhi8dw4rW0IXJ4PP7dpis',\n",
    "    'api_secret': 'o0f98faN25ofaxKjwnYDffZj3wRcL6a4',\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603424485,3fb4cc36-db0a-47ae-844e-620b936855cd',\n",
       " 'time_used': 360,\n",
       " 'faces': [{'face_token': '665ed9be43d1070c8f93a7389a021a3e',\n",
       "   'face_rectangle': {'top': 33, 'left': 127, 'width': 240, 'height': 240},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 0.033, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.004,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.021,\n",
       "     'neutral': 0.36,\n",
       "     'sadness': 99.614,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'aEXaGml7aQG0yrw+kmlBYg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "494.07px",
    "left": "22px",
    "top": "153.14px",
    "width": "243px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
